package com.yang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.common.PageQuery;
import com.yang.entity.dto.UserDTO;
import com.yang.entity.vo.UserVO;
import com.yang.mapper.UserMapper;
import com.yang.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询用户
     * @param pageQuery 分页参数及条件参数
     * @return
     */
    @Override
    public PageInfo<UserVO> pageQuery(PageQuery<UserDTO> pageQuery) {
        UserDTO params = pageQuery.getParams();
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<UserVO> result = userMapper.getUserList(params);
        if (CollectionUtils.isNotEmpty(result)) {
            return new PageInfo<>(result);
        }
        return new PageInfo<>(Collections.emptyList());
    }

    /**
     * 新增用户
     * @param userDTO
     * @return
     */
    @Override
    public int addUser(UserDTO userDTO) {
        userDTO.setId(UUID.randomUUID().toString().replace("-", ""));
        int insert = userMapper.insertUser(userDTO);
        if (insert == 1) {
            log.info("新增用户成功");
            return insert;
        }
        log.error("新增用户失败");
        return 0;
    }

}
